﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LitJson;

namespace Spun.Communications.Applications.Services
{
    public class Deserializer
    {
        public static object Map(string Json, Type Type)
        {
            if (string.IsNullOrEmpty(Json))
                return null;

            var MapperType = typeof(Mapper<>).MakeGenericType(Type);

            var Method = MapperType.GetMethod("Map");

            return Method.Invoke(null, new object[] { Json });
        }

        public static object[] Map(string JsonArgument, IEnumerable<KeyValuePair<string, Type>> Parameters)
        {
            if (string.IsNullOrEmpty(JsonArgument))
                return null;

            var JsonData = JsonMapper.ToObject(JsonArgument);
            var Mapped = new List<object>();

            foreach (var Param in Parameters)
                Mapped.Add(Map(JsonData[Param.Key], Param.Value));

            return Mapped.ToArray();
        }

        private static object Map(JsonData ParamData, Type ParamType)
        {
            if (ParamData != null)
            {
                if (ParamData.IsBoolean)
                    return (bool)ParamData;
                else if (ParamData.IsDouble)
                    return (double)ParamData;
                else if (ParamData.IsInt)
                    return (int)ParamData;
                else if (ParamData.IsLong)
                    return (long)ParamData;
                else if (ParamData.IsString)
                    return (string)ParamData;
                else if (ParamData.IsArray)
                    return MapArray(ParamData, ParamType);
                else if (ParamData.IsObject)
                    return Map(ParamData.ToJson(), ParamType);
            }

            return null;
        }

        private static object MapArray(JsonData ParamData, Type Type)
        {
            var MappedArray = Array.CreateInstance(Type.GetElementType(), ParamData.Count);

            var i = 0;
            foreach (JsonData Element in ParamData)
                MappedArray.SetValue(Map(Element, Type.GetElementType()), i++);

            return MappedArray;
        }

        private class Mapper<T>
        {
            public static object Map(string Json)
            {
                return JsonMapper.ToObject<T>(Json);
            }
        }
    }
}
